<!-- 项目审批 -->
<template>
	<view >
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">入库明细编辑</block></cu-custom>
		<view>
			<form  @submit="onSubmit">
				<view class="cu-form-group">
					<!-- <view class="title">名称</view> -->
					<uni-combox label="名称" :candidates="goodsOptions" v-model="item.goods.name" ></uni-combox>
				</view>
				<view class="cu-form-group">
					<!-- <view class="title"></view> -->
					<uni-combox label="规格" :class="!item.spec ? 'xrwaning' : ''" :candidates="item.specOptions" v-model="item.spec"></uni-combox>
				</view>
				<view class="cu-form-group">
					<!-- <view class="title">产地</view> -->
					<uni-combox label="产地" :class="!item.maker ? 'xrwaning' : ''" :candidates="item.makerOptions" v-model="item.maker"></uni-combox>
				</view>
				<view class="cu-form-group">
					<!-- <view class="title">型号</view> -->
					<uni-combox label="型号" :candidates="item.modelOptions" v-model="item.model"></uni-combox>
				</view>
				<view class="cu-form-group">
					<!-- <view class="title">材质</view> -->
					<uni-combox label="材质" :class="!item.materialCodeOptions ? 'xrwaning' : ''" :candidates="item.materialCodeOptions" v-model="item.materialCode"></uni-combox>
				</view>
				<view class="cu-form-group">
					<view class="title">车牌号</view>
					<input v-model="item.vehicleNum" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title xrequired">货位</view>
					<input :class="!item.goodsPosi ? 'xrwaning' : ''" v-model="item.goodsPosi" name="input" @blur="checkPosi()"></input>
				    <view class="cuIcon-form text-blue" @tap="selectPosi()">选择货位</view>
				</view>
				<view class="cu-form-group">
					<view class="title">堆号</view>
					<input v-model="item.heapNum" name="input"></input>
				</view>
	            <view class="cu-form-group">
	            	<uni-combox label="备注" :candidates="remarkOptions" v-model="item.remark"></uni-combox>
	            </view>
				<view class="cu-form-group">
					<view class="title">包装类型</view>
					<input v-model="item.packType" name="input"></input>
				</view>
				<view class="cu-form-group">
					<!-- <view class="title">计重方式</view> -->
					<uni-combox label="计重方式" :candidates="weightMethodOptions" v-model="item.weightMethod"></uni-combox>
				</view>
				<view class="cu-form-group">
					<!-- <view class="title">作业班组</view> -->
					<uni-combox :class=" !item.workGroup ? 'xrequiredCombox xrwaningCombox' : 'xrequiredCombox'" label="作业班组" :candidates="workGroupOptions" v-model="item.workGroup"></uni-combox>
				</view>
				<view class="cu-form-group">
					<!-- <view class="title">设备</view> -->
					<uni-combox label="设备" :candidates="machineOptions" v-model="item.machine"></uni-combox>
				</view>
				<view class="cu-form-group">
					<view class="title">保管员</view>
					<input v-model="item.tallyClerk" name="input" disabled="disabled"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">保管员班组</view>
					<input v-model="item.tallyClerkGroup" name="input" disabled="disabled"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">数量</view>
					<input v-model="item.qty" name="input"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">重量</view>
					<input v-model="item.weight" name="input"></input>
				</view>
				<view>
					<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg" :disabled="loading" type="">
						<text :class="loading ? 'cuIcon-loading2 cuIconfont-spin' : ''"></text>保存</button>
				</view>
			</form>
		</view>
		
		<view class="cu-modal" :class="isShowGoodsPosiDialog ? 'show' : ''">
			<view class="cu-dialog" :style="{ marginTop: customBarH+'px' }">
				<view class="cu-bar bg-white justify-end">
					<view class="content">选择货位</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view style="height: 350px; overflow: auto;">
					<select-goods-posi style="height: 340px;" @selected="selectedPosi"/>
				</view>
				<view class="cu-bar bg-white justify-end">
					<view class="action">
						<button class="cu-btn line-green text-green" @tap="hideModal">取消</button>
						<button class="cu-btn bg-green margin-left" @tap="hideModal">确定</button>
		
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import uniCombox from '@/components/uni-combox/uni-combox.vue'
	import selectGoodsPosi from './selectGoodsPosi'

	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			uniTag,
			uniIcons,
			uniCombox,
			selectGoodsPosi
		},
		computed: mapGetters(['user']),
		data() {
			return {
				item: { goods: {name:''}, specOptions:[], modelOptions:[], materialCodeOptions:[], makerOptions:[], weightMethod:'抄码' },
				detailId: 0,
				goodsInBillId:0,
				weightMethodOptions:['抄码','过地磅','过电子磅','理算','平均件重'],
				workGroupOptions:[],
				machineOptions:[],
				goodsOptions: [],
				remarkOptions: ['入库','落地','直装'],
				data: [],
				defaultProps: {
					children: 'children',
					label: 'fullName'
				},
				isShowGoodsPosiDialog: false,
				selectedPosiObj: {},
				loading: false,
				goods: null,
				customBarH:this.CustomBar+20,
				winHeight:0,
				windowWidth:0,
				padding:0
			}
		},
		onLoad(options) {
			
			//  高度自适应
			uni.getSystemInfo({
				success: res => {
					this.winHeight = (res.windowHeight-this.customBarH)*0.75
					this.windowWidth = res.windowWidth * 0.15
					this.padding = res.windowWidth / 750 * 20
				}
			})
			
			this.detailId = options.detailId
			this.goodsInBillId = options.goodsInBillId
			if (this.detailId >0 && this.detailId > 0) {
				this.$minApi.goodsInBill(this.goodsInBillId).then(res => {
					const data = res
					if (data) {
						const detailId = Number(this.detailId)
						const detail =  data.goodsInDetails.find(f => f.id === detailId)

						if (detail) {
							
							const machine = this.item.machine
							const weightMethod =  this.item.weightMethod
							
							this.item = detail
							this.item.tallyClerk = this.user.name
							
							if(!detail.remark){
								this.item.remark = '入库'
							}
							
							if(this.workGroupOptions && this.workGroupOptions.length>0){
								this.item.workGroup=this.workGroupOptions[0]
							}
							
							if(!detail.machine){//因为两个线程同时走 machine 先走完已经赋值了第一项， 但是明细的这个有没有 会再赋值为空
								this.item.machine=machine
							}
							if(!detail.weightMethod){
								this.item.weightMethod=weightMethod
							}	
							
							
							if(!detail.workGroup){
								this.bindWorkGroup()
							}
							
							  if(this.item.specOptions){
								  // bindSpec
								  const specOptions = this.item.specOptions.map(b => b.propertyValue)
								  this.item.specOptions = specOptions
							  }
							  
							  if(this.item.modelOptions){
								  // bindModel
								  const modelOptions = this.item.modelOptions.map(b => b.propertyValue)
								  this.item.modelOptions = modelOptions
								  
								  if(!detail.model&&modelOptions&&modelOptions.length>0){
								  	this.item.model=modelOptions[0]
								  }
							  }
				
							  if(this.item.materialCodeOptions){
								  // bindMaterialCode
								  const materialCodeOptions = this.item.materialCodeOptions.map(b => b.propertyValue)
								  this.item.materialCodeOptions = materialCodeOptions
							  }
				
							  if(this.item.makerOptions){
									// bindMaker
									const makerOptions = this.item.makerOptions.map(b => b.propertyValue)
									this.item.makerOptions = makerOptions
							  }
						}
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			} else {
				uni.setNavigationBarTitle({
				title: '新增入库明细'
				})
			}
						
			this.$minApi.getWorkGroups().then(res => {
				const data = res
				if (data) {
					this.workGroupOptions =	data.map(a => a.name)
					if(this.workGroupOptions && this.workGroupOptions.length>0){
						this.item.workGroup=this.workGroupOptions[0]
					}
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})
			
			this.$minApi.getMachines().then(res => {
				const data = res
				if (data) {
					this.machineOptions = data.map(a => a.name)
					if(this.machineOptions&&this.machineOptions.length>0&&!this.item.machine){
						this.item.machine=this.machineOptions[0]
					}					
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})
			
			const params = {
				filter: '',
				sorting: '',
				pageIndex: '10000',
				pageSize: 10
			  }
			this.$minApi.getGoods(params).then(res => {
				const data = res.list
				if (data) {
					this.goods = data
					this.goodsOptions = data.map(a => a.name)
				}
			}).catch(err => {
				console.log(err)
			}).finally(() => {
				this.isLoading = false
			})

		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			onSubmit(e) {  
				
				// if(!this.item.goodsPosi || !this.item.workGroup) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 	    title: '必填项未填写',
				// 	    duration: 2000
				// 	})
				// 	return
				// }
				
				this.loading = true
				const _this = this
				if (this.detailId > 0) {
					this.item.qty = Number(this.item.qty)
					this.item.weight = Number(this.item.weight)
					this.item.goodsInBillId =  Number(this.goodsInBillId)
					
					_this.$minApi.updateGoodsInDetail(this.item.id, this.item).then(res => {
						uni.showToast({
						    title: '修改成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsInBill/detail?id=${this.goodsInBillId}&currDetailId=${this.detailId}`
							})
						}, 2000)
				
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '修改失败',
						    duration: 2000
						})
					}).finally(() => {
						this.loading = false
					})
				} else {
					
					this.item.qty = Number(this.item.qty)
					this.item.weight = Number(this.item.weight)
					this.item.goodsInBillId =  Number(this.goodsInBillId)
					
					// this.item.goodsInDetailID = Number(this.item.goodsInDetailID)
					_this.$minApi.createGoodsInDetail(this.item).then(res => {
						uni.showToast({
						    title: '添加成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsInBill/detail?id=${this.goodsInBillId}&currDetailId=${this.detailId}`
							})
						}, 2000)
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '添加失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				}
				
			},
			hideModal(e) {
				if (this.selectedPosiObj && this.selectedPosiObj.data) {
					
					this.item.goodsPosiID = this.selectedPosiObj.data.id
					this.item.goodsPosi = this.selectedPosiObj.data.fullName
				}
				this.isShowGoodsPosiDialog = false
			},
			checkPosi() {
				
				this.$minApi.getWareHouseFullName({ fullName: this.item.goodsPosi }).then((res) => {
					if(res){
						
					}else{
						uni.showToast({
							icon: 'none',
						    title: '货位不存在',
						    duration: 2000
						})
						this.item.goodsPosi = ''
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => { })

			},
			selectPosi() {	
				this.isShowGoodsPosiDialog = true
			},
			selectedPosi(val) {
				this.selectedPosiObj = val
			},
			selectedGoods(val) {
				if (this.goods && val) {
					const goods = this.goods.find(f => f.name === val)
					
					if (goods) {
						
						this.item.goodsId = goods.id
						this.item.qtyUnit = goods.qtyUnit
						this.item.weightUnit = goods.weightUnit
						
						this.item.spec = ''
						this.item.model = ''
						this.item.materialCode = ''
						this.item.maker = ''
						
						this.bindSpec(this.item, goods.id)
						this.bindModel(this.item, goods.id)
						this.bindMaterialCode(this.item, goods.id)
						this.bindMaker(this.item, goods.id)
					}
				}
				
			},
			bindSpec(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '规格',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.specOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindModel(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '型号',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.modelOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindMaterialCode(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '材质',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.materialCodeOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			bindMaker(item, goodsID) {
			  this.$minApi.getGoodsPropertys({
				filter: '产地',
				goodsID: goodsID,
				pageIndex: 10000,
				pageSize: 10
			  }).then((res) => {
				const options = res.list.map(a => a.propertyValue)
				item.makerOptions = options
			  }).catch(err => {
				console.log(err)
			  }).finally(() => { })
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/todo/goodsInBill/detail?id=${this.goodsInBillId}&currDetailId=${this.detailId}`
				})
			},
			bindWorkGroup(){//绑定单据 默认当日保管员做第一单的 作业班组
				const params = {
					tallyClerk: this.user.name
				  }
				this.$minApi.getGoodsInDetail(params).then(res => {
					const data = res
					
					if (data) {
						this.item.workGroup = data.workGroup
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			}
		}
	}
</script>

<style scoped>
	.xrequired::after  {
		content: '*';
		color: red;
	}
.xrwaning{
	border: 1px solid #FF0000;
}
/deep/ .xrequiredCombox .uni-combox__label span::after  {
	content: '*';
	color: red;
}
/deep/ .xrequiredCombox .uni-combox__label text::after  {
	content: '*';
	color: red;
}
/deep/.xrwaningCombox input{
	border: 1px solid #FF0000;
}
</style>
